<script>
	let debug = false
	const syczuanNotice = uni.requireNativePlugin("syczuan-notice");
	import $store from '@/store/index.js';
	const linAlive = uni.requireNativePlugin("Lin-Alive");
	import {
	    UTSKeepalive
	} from "@/uni_modules/wrs-uts-keepalive"
	export default {
		onLaunch: function() {
			let systemInfo = uni.getSystemInfoSync();
			if (systemInfo.platform === 'android') {
				var Color = plus.android.importClass("android.graphics.Color");
				plus.android.importClass("android.view.Window");
				var mainActivity = plus.android.runtimeMainActivity();
				var window_android = mainActivity.getWindow();
				window_android.setNavigationBarColor(Color.argb(255, 12, 13, 26));
			}
			if(uni.$u.os() == 'android'){
				this.keepAlive()
			}else{
				this.iosKeepAlive()
			}
			// this.handlerLaunchMsg();

		},
		onShow: function() {
			syczuanNotice.deleteAll()
			uni.$appShow = true
			console.log('App Show', uni.$appShow)
		},
		onHide: function() {
			uni.$appShow = false
			console.log(uni.$appShow);
			console.log('App Hide', uni.$appShow)
		},
		onExit: function() {
			console.log('App Exit')
		},
		methods: {
			iosKeepAlive(){
				let ioskeepalive = new UTSKeepalive()
				var authorizationStatus = UTSKeepalive.getAuthorizationStatus(); 
				if (authorizationStatus == 0) {// 没有授权过定位时，请求定位权限
					ioskeepalive.requestLocationAuthorization()
				}
				
				// 监听app前后台状态
				UTSKeepalive.listenerApp((resp) => {
					console.log(resp);
					let opt = resp.opt
					switch (opt) {
						// app进入后台
						case "enterBackground": {
							console.log("播放音乐保活")
							UTSKeepalive.setCategory("playback")
							var url = plus.io.convertLocalFileSystemURL('_www/static/preview.mp3');
							console.log(url);
							ioskeepalive.playMusic({
								url: url,
								numberOfLoops: -1, // 循环此时，-1表示无限循环
								volume: 0 // 音量，0
							});
							UTSKeepalive.beginBackgroundTask()
						}
						break;
						// app回到前台
						case "enterForeground": {
							// 停止播放音乐
							ioskeepalive.stopMusic()
							// 停止定位
							// ioskeepalive.stopLocation()
							// 通知系统结束后台任务
							UTSKeepalive.endBackgroundTask()
						}
				
						break;
						default:
							break;
					}
				})
			},
			keepAlive() {
				linAlive.ignoreBatteryOptimization(res => {
				  console.log(res);
				});
				linAlive.startAliveService({
				  title: "卡速腾",
				  description: "正在后台运行",
				  hideFromTaskList: false,
				  sendStatusWhenScreenOn: true,
				  sendStatusScreenOnTime: 10,
				  enableWakeLock: true,
				  wakeLockScreenTime: 1
				}, res => {
				  console.log(res);
				});
			},
			// 打开app对应页面
			// openAppPages(res) {
			// 	if (res?.payload?.data && uni.getStorageSync('token')) {
			// 		let item = res.payload.data
			// 		const activeChatUuid = $store.state.chat.chatInfo && $store.state.chat.chatInfo.uuid || ''; // 访问根级状态的chatInfo
			// 		if (activeChatUuid != item.chat_id) {
			// 			this.$Router.pushTab('/pages/index/index')
			// 			if (item.chat_type == 'private') {
			// 				this.$Router.push({
			// 					name: 'chatMsg',
			// 					params: {
			// 						friend_idx: item.chat_id,
			// 						nick_name: item.chat_name,
			// 						avatar_url: item.chat_face,
			// 						user_type: item.chat_member_type,
			// 						skipAgain: true, //再次进入聊天页面
			// 					}
			// 				});
			// 			} else {
			// 				this.$Router.push({
			// 					name: 'chatGroupMsg',
			// 					params: {
			// 						group_id: item.chat_id,
			// 						group_name: item.chat_name,
			// 						avatar_url: item.chat_face,
			// 						skipAgain: true, //再次进入聊天页面
			// 					}
			// 				});
			// 			}
			// 		}
			// 	}
			// },
			// // 获取runtime.arguments信息
			// getArgumentsMsg() {
			// 	const args = plus.runtime.arguments;
			// 	console.log(args);
			// 	if (args) {
			// 		try {
			// 			const data = JSON.parse(args);
			// 			// 在此处理点击通知后的操作
			// 			if (data.syczuanNotice) {
			// 				const dataJson = JSON.parse(data.syczuanNotice);
			// 				this.openAppPages(dataJson);
			// 			}
			// 		} catch (error) {}
			// 	}
			// },
			// // 处理Launch信息
			// handlerLaunchMsg() {
			// 	const platform = uni.getSystemInfoSync().platform;
			// 	switch (platform) {
			// 		// case "ios":
			// 		// 	const args = plus.runtime.arguments;
			// 		// 	uni.onPushMessage((res) => {
			// 		// 		console.log(res);return
			// 		// 		if (res?.data?.payload?.syczuanNotice) {
			// 		// 			const dataJson = res.data.payload.syczuanNotice;
			// 		// 			this.openAppPages(dataJson);
			// 		// 		}
			// 		// 	});
			// 		// 	break;
			// 		// case "android":
			// 		// 	// app未在后台时
			// 		// 	// this.getArgumentsMsg();
			// 		// 	// app在后台时
			// 		// 	plus.globalEvent.addEventListener("newintent", (e) => {
			// 		// 		this.getArgumentsMsg();
			// 		// 	});
			// 		// 	break;
			// 		default:
			// 			break;
			// 	}
			// },
		},
	}
</script>
<style>
	/* #ifndef APP-PLUS-NVUE */
	page {
		background-color: #121325;
		color: #fff;
		min-height: 100%
	}

	/* #endif */

	.emoji {
		margin-bottom: -3px;
	}

	.atExtclass {
		margin-bottom: -3px;
	}

	.msgText-class {
		word-wrap: break-word;
		overflow-wrap: break-word;
	}
</style>
<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";

	.pageview {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.pageContent {
			flex: 1;
			overflow-y: auto;
		}

		.listview {
			flex: 1;
			overflow: hidden;
		}
	}
</style>